import java.util.Scanner;

public class SecuredNotepad extends SimpleNotepad {

	private String password;
	Scanner input = new Scanner(System.in);

	public SecuredNotepad(int size) {
		super(size);
		setPassword();
	}

	@Override
	public void addTextToPage(String text, int page) {
		if (checkPassword()) {
			super.addTextToPage(text, page);
		} else {
			System.out.println("Try Again");
			addNewTextToPage(text, page);
		}
	}

	@Override
	public void addNewTextToPage(String text, int page) {
		if (checkPassword()) {
			super.addNewTextToPage(text, page);
		} else {
			System.out.println("Try Again");
			addNewTextToPage(text, page);
		}
	}

	@Override
	public void deleteTextFromPage(int page) {
		if (checkPassword()) {
			super.deleteTextFromPage(page);
		} else {
			System.out.println("Try Again");
			deleteTextFromPage(page);
		}
	}

	@Override
	public void printAllPages() {
		if (checkPassword()) {
			super.printAllPages();
		} else {
			System.out.println("Try Again");
			printAllPages();
		}
	}

	@Override
	public void searchWord(String word) {
		if (checkPassword()) {
			super.searchWord(word);
		} else {
			System.out.println("Try Again");
			searchWord(word);
		}
	}

	@Override
	public void printAllPagesWithDigits() {
		if (checkPassword()) {
			super.printAllPagesWithDigits();
		} else {
			System.out.println("Try Again");
			printAllPagesWithDigits();
		}
	}

	public String getPassword() {
		return password;
	}

	private void setPassword() {
		System.out.println("Enter New Password: ");
		String tempPassword = input.nextLine();
		if (tempPassword != null || !tempPassword.equals("")) {
			if (tempPassword.length() >= 5 && tempPassword.matches(".*[A-Z].*")
					&& tempPassword.matches(".*[a-z].*")
					&& tempPassword.matches(".*\\d.*")) {
				password = tempPassword;
			} else {
				System.out
						.println("You have to type lower case upper case and number");
				setPassword();
			}
		} else
			setPassword();
	}

	public boolean checkPassword() {
		System.out.println("Enter Your Password ");
		String enteredPassword = input.nextLine();
		if (enteredPassword.equals(password)) {
			return true;
		} else {
			System.out.println("WRONG PASSWORD");
			return false;
		}

	}

}
